package com.example.springboot.common;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 标题: Result
 * 作者:  程序员子凡
 * 日期: 2024/1/22 9:31
 * 功能: 封装json数据方式2
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> {

    public static final Integer CODE_SUCCESS = 20000;
    public static final Integer CODE_AUTH_ERROR = 401;
    public static final Integer CODE_SYS_ERROR = 500;

    /*
     * 请求返回码 200 401 404 500
     * */
    private Integer code;
    /**
     * msg表示错误的详细信息
     */
    private String  msg;
    /**
     * 数据从什么地方返回出去
     * 就是这个data
     * user Object类型就是user
     * list object类型就是list
     * Map object类型就是Map
     * object
     */
    private T data;


    public static<T> Result<T> success(){
        return new Result<>(CODE_SUCCESS,"success",null);
    }

    public static<T> Result<T> success(String msg){
        return new Result<>(CODE_SUCCESS,msg,null);
    }

    public static<T> Result<T> success(T data,String msg){
        return new Result<>(CODE_SUCCESS,msg,data);
    }

    public static<T> Result<T> success(T data){
        return new Result<>(CODE_SUCCESS,"success",data);
    }

    public static<T> Result<T> error(){
        return new Result<>(CODE_SYS_ERROR,"fail",null);
    }
    public static<T> Result<T> error(Integer code){
        return new Result<>(code,"fail",null);
    }

    public static<T> Result<T> error(Integer code,String msg){
        return new Result<>(code,msg,null);
    }

    public static<T> Result<T> error(String msg){
        return new Result<>(CODE_SYS_ERROR,msg,null);
    }








}
